<?php

namespace App\Http\Controllers\Api;

use App\Models\About;
use App\Models\Suggestion;
use App\Services\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class CommonController extends Controller
{
    /**
     * 关于我
     */
    public function about()
    {
        $data = About::whereType(About::TYPE_ABOUT)
            ->pluck('meta_value', 'meta_key')->toArray();

        return Api::success([
            'phone' => $data['phone'] ?? "13212341234",
            'email' => $data['email'] ?? "example@email.com",
            'service_agreement' => $data['service_agreement'] ?? "https://example.com/agreement.html",
        ]);
    }

    /**
     * 线下转账信息
     */
    public function offline()
    {
        $data = About::whereType(About::TYPE_OFFLINE)
            ->pluck('meta_value', 'meta_key')->toArray();

        return Api::success([
            'account_name' => $data['account_name'] ?? "保定市月上服饰制造有限公司",
            'account_no' => $data['account_no'] ?? "678 7890 6789 6789",
            'account_bank' => $data['account_bank'] ?? "保定市某某支行",
            'account_bank_no' => $data['account_bank_no'] ?? "9087 3456 7777",
        ]);
    }

    /**
     * 上传我的建议
     */
    public function suggestion(Request $request)
    {
        $data = $request->all([
            'mobile',
            'email',
            'third_party',
            'content',
            'type',
        ]);

        if ($data['type'] === null) {
            return Api::msg('api.suggestion.type');
        }

        if (!$data['mobile'] && $data['email'] && $data['third_party']) {
            return Api::msg('api.suggestion.contact');
        }

        if (!$data['content']) {
            return Api::msg('api.suggestion.content');
        }

        $data = array_merge($data, ['customer_id' => Auth::id()]);

        Suggestion::create($data);

        return Api::success();
    }
}
